package my2Cents.tools;

import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.util.Vector;

import my2Cents.Sandbox;
import my2Cents.Util;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.shapes.PCircle;
import net.phys2d.raw.shapes.PConvexPolygon;
import net.phys2d.raw.shapes.PPolygon;

public class PolyDrawTool extends AbstractBodyMaker {
	private Vector vertTemp = new Vector();
	
	public PolyDrawTool() {
		setName("Polyline");
		setDescription(
				"Click and drag to draw a polyline\n" +
				"to finish, draw back to the start point."
			);
	}
	
	public void startTool(MouseEvent arg0) {
		super.startTool(arg0);
		makePolygonLine(arg0);
	}
	
	public void toolThink(MouseEvent arg0) {
		super.toolThink(arg0);
		makePolygonLine(arg0);
	}
	
	private void makeLineThick(Vector2f[] verts) {
		int len = vertTemp.size();
		for(int i=0; i<len; i++) {
			int outer = i;
			System.out.println("outer="+outer);
			Vector2f vert = (Vector2f) verts[outer];
			Vector2f vert2 = new Vector2f(vert);
			Vector2f lvert;
			int mul = 1;
			if(outer > 0) {
				lvert = (Vector2f) verts[outer-1];
			} else {
				lvert = (Vector2f) verts[len-1];
				mul = 2;
			}
			double vertNormal = (Util.angleVec(vert, lvert));
			vert2.x += Math.cos(vertNormal+(90*mul))*24;
			vert2.y += Math.sin(vertNormal+(90*mul))*24;
			
			int inner = (len+len)-i;
			System.out.println("inner="+inner);
			verts[inner] = vert2;
		}
		verts[len] = new Vector2f(verts[0]);
	}
	
	private void makePolygonLine(MouseEvent arg0) {
		Vector2f v = new Vector2f();
		v.set(arg0.getX(), arg0.getY());
		vertTemp.add(v);
		
		Vector2f first = (Vector2f) vertTemp.firstElement();
		if(vertTemp.size() > 3 && v.distance(first) < 8) {
			vertTemp.remove(v);
			Vector2f[] verts = new Vector2f[(vertTemp.size())];
			for(int i=0; i<vertTemp.size(); i++) {
				verts[i] = (Vector2f) vertTemp.get(i);
			}
			PPolygon p = new PPolygon(verts);
			if(p.getArea() < 10) {
				return;
			}
			Vector2f center = p.getCentroid();
			System.out.println("-Mass(BC): " + p.getArea()/50);
			
			center.add(Sandbox.getCameraPos());
			System.out.println(center);
			if(center.x < 0 || center.y < 0) {
				Vector2f[] newVerts = new Vector2f[verts.length];
				for(int i=0; i<verts.length; i++) {
					newVerts[i] = verts[(verts.length - i)-1];
				}
				verts = newVerts;
				p = new PPolygon(verts);
				center = p.getCentroid();
				System.err.println("INVERSION!");
			}
			center.sub(Sandbox.getCameraPos());
			
			center = p.getCentroid();
			center.set(p.computeCentroid());
			//center.add(Sandbox.getCameraPos());
			
			System.out.println("Created Polygon:");
			System.out.println("-Mass: " + p.getArea());
			System.out.println("-Convex: " + p.isConvex());
			if(p.isConvex()) {
				p = new PConvexPolygon(verts);
			}
			for(int i=0; i<p.getVertices().length; i++) {
				Vector2f vert = (Vector2f) p.getVertices()[i];
				vert.x -= center.x;
				vert.y -= center.y;
			}
			shape = p;
			Body body = createBody(p.getArea()/50);
			body.setPosition(center.x, center.y);
			sandbox.postBodyCreated(body);
			vertTemp.removeAllElements();
			endTool(arg0);
		}
	}
	
	public boolean canDraw() {
		return true;
	}
	
	public GeneralPath getGhostPath() {
		GeneralPath gp = new GeneralPath();
		if(vertTemp.size() > 0) {
			Vector2f first = (Vector2f) vertTemp.get(0);
			gp.moveTo(first.x,first.y);
			for(int i=1; i<vertTemp.size(); i++) {
				Vector2f v1 = (Vector2f) vertTemp.get(i);
				gp.lineTo(v1.x,v1.y);
			}
		}
		return gp;
	}

}
